package com.example.flightsearch.di

import android.content.Context
import com.example.flightsearch.data.AirportDao
import com.example.flightsearch.data.AirportRepository
import com.example.flightsearch.data.FavoriteDao
import com.example.flightsearch.data.FavoriteRepository
import com.example.flightsearch.data.FlightSearchDatabase
import com.example.flightsearch.data.OfflineAirportRepository
import com.example.flightsearch.data.OfflineFavoriteRepository
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class FlightSearchModule {
    @Singleton
    @Binds
    abstract fun bindAirportRepository(airportRepository: OfflineAirportRepository): AirportRepository

    @Singleton
    @Binds
    abstract fun bindFavoriteRepository(favoriteRepository: OfflineFavoriteRepository): FavoriteRepository
}

@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
    @Singleton
    @Provides
    fun provideFlightSearchDatabase(
        @ApplicationContext context: Context
    ): FlightSearchDatabase = FlightSearchDatabase.getDatabase(context)

    @Provides
    fun provideAirportDao(
        flightSearchDatabase: FlightSearchDatabase
    ): AirportDao = flightSearchDatabase.airportDao()

    @Provides
    fun provideFavoriteDao(
        flightSearchDatabase: FlightSearchDatabase
    ): FavoriteDao = flightSearchDatabase.favoriteDao()
}